"""
微信自动添加好友
"""
import subprocess
import time
import xlrd2
import uiautomation as auto

'''
待添加好友文件地址
文件第一列为电话号码，第二列为姓名
'''
infor = xlrd2.open_workbook("C:/Users/1.1/Desktop/工作簿1.xlsx")
sheet = infor.sheet_by_name("Sheet1")
phonenumbers=sheet.col_values(1)
name=sheet.col_values(0)

'''
微信安装路径
'''
subprocess.Popen('D:\微信\WeChat\WeChat.exe')

wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')


for a, j in zip(name,phonenumbers):

    k = int(j)
    i = str(k)

    messageText0 = wechatWindow.ButtonControl(Name='聊天')
    messageText0.Click()
    messageText0.Click()

    messageText1=wechatWindow.ButtonControl(Name='通讯录')
    messageText1.Click()

    messageText2=wechatWindow.ButtonControl(Name='添加朋友')
    messageText2.Click()

    search = wechatWindow.EditControl(Name='微信号/手机号')
    search.Click()
    try:
        search.GetParentControl().GetChildren()[1].SendKeys(i)
        time.sleep(0.1)

        table=wechatWindow.TextControl(Name=i)
        table.Click()

        pane=auto.PaneControl(Name='微信')
        pane.Click()

        button=pane.ButtonControl(Name='添加到通讯录')
        button.Click()
    except Exception as e:
        print(a+i+"号码已添加或不能通过手机号加好友")
        continue

    try:
        wechatWindow2 = auto.WindowControl(Name='添加朋友请求')
        wechatWindow2.Click()

        text1=wechatWindow2.EditControl(Name='')
        text1.Click()
        for k in range(20):
            text1.SendKeys('{Back}',0.00001,0.00000001)
        text1.SendKeys(a)
        button2=wechatWindow2.ButtonControl(Name='确定')
        button2.Click()
    except Exception as e:
        print('')


print("添加完成")

